import api from '../../api';
import _ from 'lodash';
import config from '../../comps/business/tradingMarket/config';

export default {
  namespace: 'securitiesCodeM',
  state: {},
  effects: {
    // 根据查询条件,获取代码(ETF、债券、基金、非标、理财直融、全部)列表
    *getSecuritiesList({ payload, callback }, { call, put, select }) {
      let args = _.cloneDeep(payload);
      delete args.codeType;
      // 后台返回前100条数据
      const res = yield call(api.getSecuritiesList[payload.codeType], args);
      if (res && res.code == 0) {
        let dataSource = [];
        let dataSourceLength = res.data?.dataSource?.length;
        for (let i = 0; i < dataSourceLength; i++) {
          if (i >= 100) break; // 取前100条数据
          let item = res.data.dataSource[i];
          let temp = config.filter((ele) => {
            return ele.value === item.exchangeCode;
          });
          let tradingMarket = '';
          if (temp && temp.length) {
            //因股指期货 交易市场121 与期权里的交易市场有交集 能查出来两条数据 取任一条 得到交易市场名称
            tradingMarket = temp[0].title;
          }
          // 交易所代码与证券代码拼接起来作为唯一ID，防止不同交易所下有相同的证券代码
          item.value = `${item.exchangeCode}-${item.tradingCode}`;
          switch (item.securityType) {
            case '7':
              item.title = `${item.tradingCode} - ${item.secuabbr} - 理财 - ${tradingMarket}`;
              break;
            case 'lczr':
              item.title = `${item.tradingCode} - ${item.secuabbr} - 理财 - ${tradingMarket}`;
              break;
            case '12':
              item.title = `${item.tradingCode} - ${item.secuabbr} - 非标 - ${tradingMarket}`;
              break;
            case 'fb':
              item.title = `${item.tradingCode} - ${item.secuabbr} - 非标 - ${tradingMarket}`;
              break;
            case '20':
              item.title = `${item.tradingCode} - ${item.secuabbr} - 资产管理计划 - ${tradingMarket}`;
              break;
            case '21':
              item.title = `${item.tradingCode} - ${item.secuabbr} - 委外 - ${tradingMarket}`;
              break;
            default:
              item.title = `${item.tradingCode} - ${item.secuabbr} - ${tradingMarket}`;
              break;
          }

          dataSource.push(item);
        }
        callback(dataSource);
      }
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
